/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.security;

import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.ognl.OgnlException;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.vacoor.nothing.common.json.Jacksons;
import org.vacoor.nothing.web.entity.Variable;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Map;

/**
 * @author vacoor
 */
public class HbnSchemaExp {
    public static void main(String[] args) throws OgnlException, IOException {
        InputStream is = Resources.getResourceAsStream("config.json");
        Map<String, Variable> vars = Jacksons.deserialize(is, new TypeReference<Map<String, Variable>>() {
        });
        for (Variable v : vars.values()) {
            System.out.println(
                    String.format(
                            "INSERT INTO T_CONFIG(VAR_NAME, VAR_DESC, VAR_TYPE, VAR_VALUE, VAR_GROUP, VAR_ORDER) VALUES('%s', '%s', '%s', '%s', '%s', %s);",
                            v.getName(),
                            v.getDesc(),
                            v.getType(),
                            v.getValue(),
                            v.getGroup(),
                            v.getOrder()
                    ));
        }
        System.exit(0);
        /*
        Map<String, Object> ctx = Maps.newHashMap();

        Map<String, Object> params = Maps.newHashMap();
        ctx.put("business", params);
        params.put("_filters", Filters.create());
//        params.put("hehe", "xx");
        // 只有空 filters
        Object value = Ognl.getValue("business != null and (business._filters == null ? business.size > 0 : (business.size > 1 or business._filters.size() > 0)) ? 'INNER JOIN' : 'LEFT JOIN'", ctx, ctx);
        System.out.println(value);

        System.exit(0);
        */

        Configuration config = new Configuration();
        config.setProperty("hbm2ddl.auto", "update");
        config.setProperty("hibernate.show_sql", "true");
        config.setProperty("hibernate.format_sql", "true");
//        config.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
        config.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        /*
        config.setProperty("connection.driver_class","oracle.jdbc.OracleDriver");
        config.setProperty("connection.url", "jdbc:oracle:thin:@192.168.125.131:1521:orcl");
        config.setProperty("connection.username", "vacoor");
        config.setProperty("connection.password", "");
        config.setNamingStrategy(UpperCaseUnderscoreNamingStrategy.INSTANCE);
        */

//        config.addAnnotatedClass(User.class);
//        config.addAnnotatedClass(Role.class);
//        config.addAnnotatedClass(Permission.class);
        config.addAnnotatedClass(Variable.class);


        SchemaExport export = new SchemaExport(config);
        export.setDelimiter(";");
        export.setFormat(true);
        export.setHaltOnError(true);
        export.create(true, false);
    }
}
